<?php
namespace Home\Controller;
use Think\Controller;
class BatchController extends ZMController {
	private static $apiKey='Z0cdoCXULiMdCx9k';//秘钥
	/*public function batchYearTypeCollect(){//更新yearTypeCollect接口数据
		$apiKey=self::$apiKey;
		$pdate=date('Y-m-d H:i:s');
		$apiKey=base64_encode(md5($pdate.$apiKey));
		$region=array(0,1,2,3,4,5,6,7,8,9,10,11,12,13,999);
		foreach($region as $v){
			unset($data);
			$data['pdate']=$pdate;
			$data['apiKey']=$apiKey;
			$data['region']=$v;
			$url="http://localhost/hzxt/Message/yearTypeCollect";
			https_request($url,$data);//循环更新
		}
	}
	public function batchMonthCollect(){//更新monthCollect接口数据
		$apiKey=self::$apiKey;
		$pdate=date('Y-m-d H:i:s');
		$apiKey=base64_encode(md5($pdate.$apiKey));
		$region=array(0,1,2,3,4,5,6,7,8,9,10,11,12,13,999);
		foreach($region as $v){
			unset($data);
			$data['pdate']=$pdate;
			$data['apiKey']=$apiKey;
			$data['region']=$v;
			$url="http://localhost/hzxt/Message/monthCollect";
			https_request($url,$data);//循环更新
		}
	}*/
	public function _initialize(){
		$this->region=999;
	}
	public function collectOver(){//整合收集数据
		try{
			$region=$this->region;
			$hid=NULL;
			$r=R('Cust/hospital',array($hid,$region));//所有医院
			$date=date('Y-m-d',strtotime('-1 day'));
			$mdate=date('Y-m',strtotime($date));
			$Collect=M()->db(1,'DB_CONFIG1');
			$over=M()->db(1,'DB_CONFIG1');
			M()->db(1,'DB_CONFIG1')->startTrans();
			$type=array(0,1,3,4,5,6,7);
			foreach($r as $v){
				$hid=$v['hid'];
				unset($where);
				$where['uploaddate']=$date;
				$where['hid']=$hid;
				$res=$Collect->table("ddwk_collect")->where($where)->field("hid,cid,pdate,nval1_2,nval1_5")->order('pdate asc')->select();//这个医院今天上传的所有数据
				$arr=array();
				if(!empty($res)){
					foreach($res as $row){
						if(!isset($arr[$row['pdate']])){
							$arr[$row['pdate']] = $row;
						}
						else{
							$arr[$row['pdate']]['cid'].=','.$row['cid'];
							$arr[$row['pdate']]['nval1_2']+=$row['nval1_2'];
							$arr[$row['pdate']]['nval1_5']+=$row['nval1_5'];
						}
					}
					$arr=array_values($arr);
				}
				else{
					$arr=array(array("hid"=>$hid,"pdate"=>$date));
				}
				$ppdate='0000-00';
				$weight['weight0']=0;
				$weight['weight1']=0;
				$weight['weight3']=0;
				$weight['weight4']=0;
				$weight['weight5']=0;
				$weight['weight6']=0;
				$weight['weight7']=0;
				$weight['nval1_2']=0;
				$weight['nval1_5']=0;
				foreach($arr as $p){
					$pdate=date('Y-m',strtotime($p['pdate']));
					//if($pdate!=$ppdate){
						unset($w);
						unset($weight);
						$w['hid']=$p['hid'];
						$w['pdate']=$pdate;
						$m=$over->table("ddwk_collect_over")->where($w)->find();//查出这个月以保存的数据
						if(!empty($m)){//存在
							foreach($type as $t){
								$we=R('Collect/totalCweight',array($p['cid'],$t));
								$weight['weight'.$t]=bcadd($m['weight'.$t],$we,2);//重量
							}
							$weight['nval1_2']=$m['nval1_2']+$p['nval1_2'];
							$weight['nval1_5']=$m['nval1_5']+$p['nval1_5'];
							if($mdate==$pdate){
								$weight['ppdate']=$date;
							}
							$over->table("ddwk_collect_over")->where($w)->save($weight);//修改
						}
						else{//不存在
							foreach($type as $t){
								$weight['weight'.$t]=R('Collect/totalCweight',array($p['cid'],$t));//重量
							}
							$weight['nval1_2']=$p['nval1_2'];
							$weight['nval1_5']=$p['nval1_5'];
							$weight['hid']=$p['hid'];
							$weight['pdate']=$pdate;
							$weight['ppdate']=$date;
							$over->table("ddwk_collect_over")->add($weight);//新增
						}
						//$ppdate=$pdate;
					//}
				}
			}
			M()->db(1,'DB_CONFIG1')->commit();
		}
		catch(\Exception $e){
			$message=$e->getMessage();
			M()->db(1,'DB_CONFIG1')->rollback();
			var_dump($message);
		}
	}
	public function collectOverLog(){//处理日志里的删减
		try{
			M()->db(1,'DB_CONFIG1')->startTrans();
			$Model=M();
			$date=date('Y-m-d',strtotime('-1 day'));
			$where['date_format(uploaddate,"%Y-%m-%d")']=$date;
			$where['pdate']=array('NEQ',$date);
			$log=$Model->table('ddwk_log')->where($where)->order("id asc")->select();
			$data=array();
			$w=array();
			foreach($log as $row){
				$flag=0;
				$w['hid']=$row['hid'];
				$w['pdate']=date('Y-m',strtotime($row['pdate']));
				$collectover=$Model->table('ddwk_collect_over')->where($w)->find();
				$data['weight0']=$collectover['weight0'];
				$data['weight1']=$collectover['weight1'];
				$data['weight4']=$collectover['weight4'];
				$data['weight5']=$collectover['weight5'];
                $data['nval1_2']=$collectover['nval1_2'];
                $data['nval1_5']=$collectover['nval1_5'];
				if(!empty($row['addweight0'])){//需要操作
					$data['weight0']=bcadd($data['weight0'],$row['addweight0'],2);
					$flag=1;
				}
				if(!empty($row['addweight1'])){//需要操作
					$data['weight1']=bcadd($data['weight1'],$row['addweight1'],2);
					$flag=1;
				}
				if(!empty($row['addweight4'])){//需要操作
					$data['weight4']=bcadd($data['weight4'],$row['addweight4'],2);
					$flag=1;
				}
				if(!empty($row['addweight5'])){//需要操作
					$data['weight5']=bcadd($data['weight5'],$row['addweight5'],2);
					$flag=1;
				}
                if(!empty($row['addnval1_2'])){//需要操作
                    $data['nval1_2']+=$row['addnval1_2'];
                    $flag=1;
                }
                if(!empty($row['addnval1_5'])){//需要操作
                    $data['nval1_5']+=$row['addnval1_5'];
                    $flag=1;
                }
				if(!empty($row['subweight0'])){//需要操作
					$data['weight0']=bcsub($data['weight0'],$row['subweight0'],2);
					$flag=1;
				}
				if(!empty($row['subweight1'])){//需要操作
					$data['weight1']=bcsub($data['weight1'],$row['subweight1'],2);
					$flag=1;
				}
				if(!empty($row['subweight4'])){//需要操作
					$data['weight4']=bcsub($data['weight4'],$row['subweight4'],2);
					$flag=1;
				}
				if(!empty($row['subweight5'])){//需要操作
					$data['weight5']=bcsub($data['weight5'],$row['subweight5'],2);
					$flag=1;
				}
                if(!empty($row['subnval1_2'])){//需要操作
                    $data['nval1_2']-=$row['subnval1_2'];
                    $flag=1;
                }
                if(!empty($row['subnval1_5'])){//需要操作
                    $data['nval1_5']-=$row['subnval1_5'];
                    $flag=1;
                }
				if($flag==1){
					$Model->table('ddwk_collect_over')->where($w)->save($data);
				}
			}
			M()->db(1,'DB_CONFIG1')->commit();
		}
		catch(\Exception $e){
			$message=$e->getMessage();
			M()->db(1,'DB_CONFIG1')->rollback();
			var_dump($message);
		}
	}
	public function collectOverDay(){//日合计
		try{
			$region=$this->region;
			$hid=NULL;
			$r=R('Cust/hospital',array($hid,$region));//所有医院
			$date=date('Y-m-d',strtotime('-1 day'));
			$mdate=date('Y-m',strtotime($date));
			$Collect=M()->db(1,'DB_CONFIG1');
			$over=M()->db(1,'DB_CONFIG1');
			M()->db(1,'DB_CONFIG1')->startTrans();
			$type=array(0,1,3,4,5,6,7);
			foreach($r as $v){
				$hid=$v['hid'];
				unset($where);
				$where['uploaddate']=$date;
				$where['hid']=$hid;
				$res=$Collect->table("ddwk_collect")->where($where)->field("hid,cid,pdate,nval1_2,nval1_5")->order('pdate asc')->select();//这个医院今天上传的所有数据
				$arr=array();
				if(!empty($res)){
					foreach($res as $row){
						if(!isset($arr[$row['pdate']])){
							$arr[$row['pdate']] = $row;
						}
						else{
							$arr[$row['pdate']]['cid'].=','.$row['cid'];
							$arr[$row['pdate']]['nval1_2']+=$row['nval1_2'];
							$arr[$row['pdate']]['nval1_5']+=$row['nval1_5'];
						}
					}
					$arr=array_values($arr);
				}
				else{				
					$arr=array(array("hid"=>$hid,"pdate"=>$date));
				}
				$ppdate='0000-00-00';
				$weight['weight0']=0;
				$weight['weight1']=0;
				$weight['weight3']=0;
				$weight['weight4']=0;
				$weight['weight5']=0;
				$weight['weight6']=0;
				$weight['weight7']=0;
				$weight['nval1_2']=0;
				$weight['nval1_5']=0;
				foreach($arr as $p){
					$pdate=date('Y-m-d',strtotime($p['pdate']));
					if($pdate!=$ppdate){
						unset($w);
						unset($weight);
						$w['hid']=$p['hid'];
						$w['pdate']=$pdate;
						$m=$over->table("ddwk_collect_overday")->where($w)->find();//查出这个月以保存的数据
						if(!empty($m)){//存在
							foreach($type as $t){
								$we=R('Collect/totalCweight',array($p['cid'],$t));
								$weight['weight'.$t]=bcadd($m['weight'.$t],$we,2);//重量
							}
							$weight['nval1_2']=$m['nval1_2']+$p['nval1_2'];
							$weight['nval1_5']=$m['nval1_5']+$p['nval1_5'];
							if($mdate==$pdate){
								$weight['ppdate']=$date;
							}
							$over->table("ddwk_collect_overday")->where($w)->save($weight);//修改
						}
						else{//不存在
							foreach($type as $t){
								$weight['weight'.$t]=R('Collect/totalCweight',array($p['cid'],$t));//重量
							}
							$weight['nval1_2']=$p['nval1_2'];
							$weight['nval1_5']=$p['nval1_5'];
							$weight['hid']=$p['hid'];
							$weight['pdate']=$pdate;
							$weight['ppdate']=$date;
							$over->table("ddwk_collect_overday")->add($weight);//新增
						}
						$ppdate=$pdate;
					}
				}
			}
			M()->db(1,'DB_CONFIG1')->commit();
		}
		catch(\Exception $e){
			$message=$e->getMessage();
			M()->db(1,'DB_CONFIG1')->rollback();
			var_dump($message);
		}
	}
	public function collectOverLogDay(){//处理日志里的日删减
		try{
			M()->db(1,'DB_CONFIG1')->startTrans();
			$Model=M();
			$date=date('Y-m-d',strtotime('-1 day'));
			$where['date_format(uploaddate,"%Y-%m-%d")']=$date;
			$where['pdate']=array('NEQ',$date);
			$log=$Model->table('ddwk_log')->where($where)->order("id asc")->select();
			$data=array();
			$w=array();
			foreach($log as $row){
				$flag=0;
				$w['hid']=$row['hid'];
				$w['pdate']=date('Y-m-d',strtotime($row['pdate']));
				$collectover=$Model->table('ddwk_collect_overday')->where($w)->find();
				$data['weight0']=$collectover['weight0'];
				$data['weight1']=$collectover['weight1'];
				$data['weight4']=$collectover['weight4'];
				$data['weight5']=$collectover['weight5'];
                $data['nval1_2']=$collectover['nval1_2'];
                $data['nval1_5']=$collectover['nval1_5'];
				if(!empty($row['addweight0'])){//需要操作
					$data['weight0']=bcadd($data['weight0'],$row['addweight0'],2);
					$flag=1;
				}
				if(!empty($row['addweight1'])){//需要操作
					$data['weight1']=bcadd($data['weight1'],$row['addweight1'],2);
					$flag=1;
				}
				if(!empty($row['addweight4'])){//需要操作
					$data['weight4']=bcadd($data['weight4'],$row['addweight4'],2);
					$flag=1;
				}
				if(!empty($row['addweight5'])){//需要操作
					$data['weight5']=bcadd($data['weight5'],$row['addweight5'],2);
					$flag=1;
				}
                if(!empty($row['addnval1_2'])){//需要操作
                    $data['nval1_2']+=$row['addnval1_2'];
                    $flag=1;
                }
                if(!empty($row['addnval1_5'])){//需要操作
                    $data['nval1_5']+=$row['addnval1_5'];
                    $flag=1;
                }
				if(!empty($row['subweight0'])){//需要操作
					$data['weight0']=bcsub($data['weight0'],$row['subweight0'],2);
					$flag=1;
				}
				if(!empty($row['subweight1'])){//需要操作
					$data['weight1']=bcsub($data['weight1'],$row['subweight1'],2);
					$flag=1;
				}
				if(!empty($row['subweight4'])){//需要操作
					$data['weight4']=bcsub($data['weight4'],$row['subweight4'],2);
					$flag=1;
				}
				if(!empty($row['subweight5'])){//需要操作
					$data['weight5']=bcsub($data['weight5'],$row['subweight5'],2);
					$flag=1;
				}
                if(!empty($row['subnval1_2'])){//需要操作
                    $data['nval1_2']-=$row['subnval1_2'];
                    $flag=1;
                }
                if(!empty($row['subnval1_5'])){//需要操作
                    $data['nval1_5']-=$row['subnval1_5'];
                    $flag=1;
                }
				if($flag==1){
					$Model->table('ddwk_collect_overday')->where($w)->save($data);
				}
			}
			M()->db(1,'DB_CONFIG1')->commit();
		}
		catch(\Exception $e){
			$message=$e->getMessage();
			M()->db(1,'DB_CONFIG1')->rollback();
			var_dump($message);
		}
	}
}